package com.cy;

import com.cy.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

/**
 *服务消费方对象的启动类
 * 业务描述：
 *
 */
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    public RestTemplate loadBalancedRestTemplate(){
        return new RestTemplate();
    }


    @RestController
    public class ConsumerController{
            /**
             *负载均衡
             */
            @Autowired
            private LoadBalancerClient loadBalancerClient;
        @Autowired
        private RestTemplate restTemplate;
        @Autowired
        private RestTemplate loadBalancedRestTemplate;

        @Value("${spring.application.name}")
        private String appName;

        @Autowired
        private ConsumerService consumerService;
        //构建一个线程安全并且可实现自增自减的操作的整数对象
        private AtomicLong aLong = new AtomicLong(1);
        @GetMapping("/consumer/doRestEcho1")
        public String doRestEcho1() throws InterruptedException {
            long num = aLong.getAndIncrement();
            if (num%2==0){
                //系统设计时会认为响应设计超过200毫秒为慢调用
                Thread.sleep(200);
            }

            //consumerService.doGetResource();//加了这实现链路限流时刷新页面就显示的是500，直接通过业务方法访问相关资源（现在不需要关系什么返回值）
            String url = "http://localhost:8081/provider/echo/"+appName;
            return restTemplate().getForObject(url, String.class);
        }

        @GetMapping("/consumer/doRestEcho2")
        public String doRestEcho2(){
            ServiceInstance instance = loadBalancerClient.choose("sca-provider");
            String ip = instance.getHost();
            int port = instance.getPort();
            //String url = "http://"+ip+":"+port+"/provider/echo1";
            String url = String.format("http://%s:%s/provider/echo/%s",ip,port, appName);
            return restTemplate.getForObject(url, String.class);
        }

        @GetMapping("/consumer/doRestEcho3")
        public String doRestEcho3(){
            String url = String.format("http://sca-provider/provider/echo/%s", appName);
            return loadBalancedRestTemplate.getForObject(url, String.class);
        }
    }

}
